﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using SklepISI.Core.Interfaces;
using SklepISI.Core.Models;

namespace SklepISI.Core.Repositories
{
    public class ProductRepository : IProductRepository
    {
        public ProductModel AddProduct(ProductModel product)
        {
            if (product == null) return null;

            var dbProduct = Mapper.Map<ProductModel, Product>(product);

            if (dbProduct == null) return null;

            using (var ctx = new Entities())
            {
                var result = ctx.Products.Add(dbProduct);
                ctx.SaveChanges();
                return Mapper.Map<Product, ProductModel>(result);
            }
        }

        public bool RemoveProduct(int productId)
        {
            using (var ctx = new Entities())
            {
                var product = ctx.Products.Find(productId);
                if(product != null)
                {
                    product.IsAvailable = false;
                    ctx.SaveChanges();
                    return true;
                }
            }
            return false;
        }

        public bool UpdateProduct(ProductModel product)
        {
            using (var ctx = new Entities())
            {
                var dbProduct = Mapper.Map<ProductModel, Product>(product);
                ctx.Entry(dbProduct).State = EntityState.Modified;
                ctx.SaveChanges();
                return true;
            }
        }

        public IList<ProductModel> GetProducts()
        {
            using (var ctx = new Entities())
            {
                var tmp = ctx.Products.Where(product =>product.IsAvailable).ToList();
                return Mapper.Map<IList<Product>, IList<ProductModel>>(tmp);
            }
        }
        public IList<ProductModel> GetProducts(int categoryId)
        {
            using (var ctx = new Entities())
            {
                var tmp = ctx.Products.Where(p => p.CategoryID == categoryId && p.IsAvailable).ToList();
                return Mapper.Map<IList<Product>, IList<ProductModel>>(tmp);
            }
        }


        public CategoryModel AddCategory(CategoryModel category)
        {
            if (category.Id != 0)
                throw new ArgumentException();

            var dbCategory = Mapper.Map<CategoryModel, Category>(category);
            if (dbCategory == null)
            {
                return null;
            }
            else
            {
                using (var ctx = new Entities())
                {
                    var ctgIdx = ctx.Categories.Max(c => c.ID) + 1;
                    dbCategory.ID = ctgIdx;

                    dbCategory = ctx.Categories.Add(dbCategory);
                    ctx.SaveChanges();
                    return Mapper.Map<Category, CategoryModel>(dbCategory);
                }
            }
        }
        public bool RemoveCategory(int categoryId)
        {
            throw new NotImplementedException("kasować ręcznie z bazy");
        }
        public bool UpdateCategory(CategoryModel category)
        {
            using (var ctx = new Entities())
            {
                var dbCategory = Mapper.Map<CategoryModel, Category>(category);
                ctx.Entry(dbCategory).State = EntityState.Modified;
                ctx.SaveChanges();
                return true;
            }
        }
        public IList<CategoryModel> GetCategories()
        {
            using (var ctx = new Entities())
            {
                var tmp = ctx.Categories.ToList();
                return Mapper.Map<IList<Category>, IList<CategoryModel>>(tmp);
            }
        }



        public ProductModel GetProduct(int productId)
        {
            using(var context = new Entities())
            {
                return Mapper.Map<Product, ProductModel>(context.Products.Find(productId));
            }
        }
    }
}
